﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Collections.Specialized;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class StableSpentsAdapter : BaseAdapter
    {
        private readonly Activity _context;
        private readonly List<StableMoneyFlowItemModel> _items = new List<StableMoneyFlowItemModel>();
        private readonly MoneyCalculationModel _model;

        public StableSpentsAdapter(Activity context, MoneyCalculationModel model)
            : base()
        {
            _context = context;
            _model = model;
            model.Spents.StableMoneyFlowItemsCollectionChanged += Spents_StableMoneyFlowItemsCollectionChanged;
            //model.PopulateMoneyFlowItems();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.StableSpentsItemLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, StableMoneyFlowItemModel stableMoneyFlowItemModel)
        {
            EditText nameEditText = view.FindViewById<EditText>(Resource.Id.nameEditText);
            EditText amountEditText = view.FindViewById<EditText>(Resource.Id.amountEditText);
            nameEditText.Text = stableMoneyFlowItemModel.Name;
            amountEditText.Text = stableMoneyFlowItemModel.Amount.ToString();
            nameEditText.TextChanged += (sender, e) =>
            {
                stableMoneyFlowItemModel.Name = nameEditText.Text;
                stableMoneyFlowItemModel.Save();
            };
            amountEditText.TextChanged += (sender, e) =>
            {
                decimal amount;
                if (decimal.TryParse(amountEditText.Text, out amount))
                {
                    stableMoneyFlowItemModel.Amount = amount;
                    stableMoneyFlowItemModel.Save();
                }
            };
            Button removeStableSpentsItemButton = view.FindViewById<Button>(Resource.Id.removeStableSpentsItemButton);
            removeStableSpentsItemButton.Click += (sender, e) => stableMoneyFlowItemModel.Remove();
            Spinner periodSpinner = view.FindViewById<Spinner>(Resource.Id.periodSpinner);
            if (stableMoneyFlowItemModel.GetIsMonthPeriod())
                periodSpinner.SetSelection(1);
            if (stableMoneyFlowItemModel.Period == TimeSpan.FromDays(7.0))
                periodSpinner.SetSelection(0);
            if (stableMoneyFlowItemModel.Period == TimeSpan.FromDays(365.0))
                periodSpinner.SetSelection(2);
            periodSpinner.ItemSelected += (sender, e) =>
            {
                switch (periodSpinner.SelectedItemPosition)
                {
                    case 0:
                        stableMoneyFlowItemModel.Period = TimeSpan.FromDays(7.0);
                        break;
                    case 1:
                        stableMoneyFlowItemModel.SetMonthPeriod();
                        break;
                    case 2:
                        stableMoneyFlowItemModel.Period = TimeSpan.FromDays(365.0);
                        break;
                }
                stableMoneyFlowItemModel.Save();
            };
        }

        private void Spents_StableMoneyFlowItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    _items.AddRange(e.NewItems.OfType<StableMoneyFlowItemModel>());
                    break;
                case NotifyCollectionChangedAction.Remove:
                    e.OldItems.OfType<StableMoneyFlowItemModel>().ToList().ForEach(i =>
                        _items.Remove(i));
                    break;
                default:
                    break;
            }
            NotifyDataSetChanged();
        }
    }
}